package com.kuaimacode.kframework.api.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.api.vo.response.MasterScoreDetailsResp;
import com.kuaimacode.kframework.mybatis.mapper.masterscore.MasterScoreDetailsMapper;
import com.kuaimacode.kframework.mybatis.models.masterscore.MasterScoreDetails;
import com.kuaimacode.kframework.mybatis.models.masterscore.MasterScoreLevel;
import com.kuaimacode.kframework.util.DateUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 12786 on 2019/3/17.
 * @version 1.0
 */
@Service
public class MasterScoreDetailsService extends ServiceImpl<MasterScoreDetailsMapper,MasterScoreDetails> {


    public PageInfo<MasterScoreDetailsResp> getMasterScoreDetails(Long userId, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<MasterScoreDetails> masterScoreDetails = baseMapper.findMasterScoreDetailsByUserId(userId);
        List<MasterScoreDetailsResp> masterScoreDetailsResps = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(masterScoreDetails)){
            for (MasterScoreDetails detailsResp:masterScoreDetails){
                    MasterScoreDetailsResp detailss = new MasterScoreDetailsResp();
                    detailss.setScore(detailsResp.getScore());
                    detailss.setScoreDate(DateUtil.dateToString(detailsResp.getScoreDate(),"yyyy-MM-dd"));
                    detailss.setScoreDes(detailsResp.getScoreDes());
                    masterScoreDetailsResps.add(detailss);
            }
        }
        return new PageInfo<MasterScoreDetailsResp>(masterScoreDetailsResps);
    }

    public List<MasterScoreLevel> getMasterLevelList(){
        List<MasterScoreLevel> masterScoreDetails = baseMapper.findMasterScoreGroupUser();
        return masterScoreDetails;
    }

    public void insertScore(Long userId,String desc,String orderNo,String score,String scoreType){
        MasterScoreDetails details = new MasterScoreDetails();
        Date now = new Date();
        details.setCreateDate(now);
        details.setOrderNo(orderNo);
        details.setScoreType(scoreType);
        details.setScore(String.valueOf(score));
        details.setScoreDes(desc);
        details.setUserId(userId);
        this.save(details);
    }


}
